testsuite: add widget refcount test case
authorTimm Bäder <mail@baedert.org>
Tue, 26 Sep 2017 15:04:51 +0000 (17:04 +0200)
committerMatthias Clasen <mclasen@redhat.com>
Mon, 11 May 2020 16:20:59 +0000 (12:20 -0400)
Testing toplevels and popovers.

testsuite/gtk/meson.build
testsuite/gtk/widget-refcount.c [new file with mode: 0644]

index b7440b5529ee6e17ae847c9fcbbf9e68df127dc0..9df9b0c0d8d37107ffedb0e65b0cddca426bf030 100644 (file)
@@ -66,6 +66,7 @@ tests = [
   ['displayclose'],
   ['revealer-size'],
   ['widgetorder'],
+  ['widget-refcount'],
 ]
 
 # Tests that are expected to fail
diff --git a/testsuite/gtk/widget-refcount.c b/testsuite/gtk/widget-refcount.c
new file mode 100644 (file)
index 0000000..d4fc5d8
--- /dev/null
@@ -0,0 +1,101 @@
+#include <gtk/gtk.h>
+
+static void
+check_finalized (gpointer data,
+                 GObject *where_the_object_was)
+{
+  gboolean *did_finalize = (gboolean *)data;
+
+  *did_finalize = TRUE;
+}
+
+static void
+popover (void)
+{
+  GtkWidget *button = gtk_menu_button_new ();
+  GtkWidget *p = gtk_popover_new ();
+  gboolean finalized = FALSE;
+
+  gtk_menu_button_set_popover (GTK_MENU_BUTTON (button), p);
+
+  /* GtkButton is a normal widget and thus floating */
+  g_assert (g_object_is_floating (button));
+  /* GtkPopver sinks itself */
+  g_assert (!g_object_is_floating (p));
+
+  g_object_weak_ref (G_OBJECT (p), check_finalized, &finalized);
+
+  g_object_ref_sink (button);
+  g_object_unref (button);
+  /* We do NOT unref p since the only reference held to it gets
+   * removed when the button gets disposed. */
+  g_assert (finalized);
+}
+
+static void
+popover2 (void)
+{
+  GtkWidget *button = gtk_menu_button_new ();
+  GtkWidget *p = gtk_popover_new ();
+  gboolean finalized = FALSE;
+
+  gtk_menu_button_set_popover (GTK_MENU_BUTTON (button), p);
+
+  g_assert (g_object_is_floating (button));
+  g_assert (!g_object_is_floating (p));
+
+  g_object_weak_ref (G_OBJECT (p), check_finalized, &finalized);
+
+  g_object_ref_sink (button);
+
+  gtk_menu_button_set_popover (GTK_MENU_BUTTON (button), NULL);
+
+  g_assert (finalized);
+
+  g_object_unref (button);
+}
+
+static void
+filechooserwidget (void)
+{
+  /* We use GtkFileChooserWidget simply because it's a complex widget, that's it. */
+  GtkWidget *w = gtk_file_chooser_widget_new (GTK_FILE_CHOOSER_ACTION_OPEN);
+  gboolean finalized = FALSE;
+
+  g_assert (g_object_is_floating (w));
+  g_object_ref_sink (w);
+  g_object_weak_ref (G_OBJECT (w), check_finalized, &finalized);
+
+  g_object_unref (w);
+
+  g_assert (finalized);
+}
+
+static void
+window (void)
+{
+  GtkWidget *w = gtk_window_new ();
+  gboolean finalized = FALSE;
+
+  /* GTK holds a ref */
+  g_assert (!g_object_is_floating (w));
+  g_object_weak_ref (G_OBJECT (w), check_finalized, &finalized);
+
+  gtk_window_destroy (GTK_WINDOW (w));
+
+  g_assert (finalized);
+}
+
+int
+main (int argc, char **argv)
+{
+  g_test_init (&argc, &argv, NULL);
+  gtk_init ();
+
+  g_test_add_func ("/gtk/widget-refcount/popover", popover);
+  g_test_add_func ("/gtk/widget-refcount/popover2", popover2);
+  g_test_add_func ("/gtk/widget-refcount/filechoosewidget", filechooserwidget);
+  g_test_add_func ("/gtk/widget-refcount/window", window);
+
+  return g_test_run ();
+}